<template>
  <div>
    <el-dialog
      :title="isAdd == true ? '添加管理员' : '编辑管理员'"
      :visible.sync="isShow"
      :before-close="cancel"
    >
      <el-form :model="user_info" ref="userInfo">
        <el-form-item label="所属角色" :label-width="formLabelWidth">
          <el-select v-model="user_info.roleid" placeholder="请选择">
            <el-option
              v-for="item in rolelist"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名称" :label-width="formLabelWidth">
          <el-input autocomplete="off" v-model="user_info.username"></el-input>
        </el-form-item>

        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input autocomplete="off" v-model="user_info.password"></el-input>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="user_info.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { userAdd, userEdit, getuserInfo } from "../../request/api";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      user_info: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },

      formLabelWidth: "120px",
    };
  },
  computed: {
    ...mapGetters({
      rolelist: "role/getroles",
    }),
  },

  mounted() {
    if (this.rolelist.length == 0) {
      this.getRole();
    }
  },
  methods: {
    ...mapActions({
      getUser: "user/getUserList",
      getRole: "role/getRoleList",
      getCont:'user/getContActions'
    }),
    // 清空表单
    reset() {
      this.user_info = {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      };
    },
    //封装一个取消事件
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },

    // 封装一个事件
    lookup(uid) {
      console.log(uid, 111111111111111);
      // 获取一条数据
      getuserInfo({ uid: uid }).then((res) => {
        console.log(res);
        this.user_info = res.list;
        this.user_info.uid = uid;
        console.log(this.user_info);
      });
    },
    add() {
      this.$refs.userInfo.validate((val) => {
        if (val) {
          if (this.isAdd) {
            // c菜单添加

            userAdd(this.user_info).then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.getUser();
                this.getCont()
              } else {
                this.$message.error(res.msg);
              }
              this.cancel();
            });
          } else {
            //  编辑
            userEdit(this.user_info).then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.getUser();

                // this.role_info.menus.split('')
              } else {
                this.$message.error(res.msg);
              }
              this.cancel();
            });
          }
        }
      });
    },
  },
};
</script>

<style  scoped>
</style>
